<?php
class BaseUsers extends Zend_Db_Table
{   
    protected $_name = "users";
    
    /**
    * Class constructor
    * @return void
    */    
	function __construct($config = null){
        parent::__construct();
        
        
        $this->id = 0;
        
        $this->username = "";
        
        $this->password = "";
        
        $this->fullname = "";
        
        $this->gender = 0;
        
        $this->email = "";
        
        $this->address = "";
        
        $this->telephone = "";
        
        $this->type = 0;
        
        $this->last_login = date("Y-m-d H:i:s");
        
        $this->created = date("Y-m-d H:i:s");
        
        $this->status = 0;
        
    }
	
    function clearData()
    {
        
        $this->id = 0;
        
        $this->username = "";
        
        $this->password = "";
        
        $this->fullname = "";
        
        $this->gender = 0;
        
        $this->email = "";
        
        $this->address = "";
        
        $this->telephone = "";
        
        $this->type = 0;
        
        $this->last_login = date("Y-m-d H:i:s");
        
        $this->created = date("Y-m-d H:i:s");
        
        $this->status = 0;
        
    }
    
	/**
	All property of class
	*/
    
    /*
	* @declare   id of  Users
	*/
    protected $id;
    
    /*
	* @declare   username of  Users
	*/
    protected $username;
    
    /*
	* @declare   password of  Users
	*/
    protected $password;
    
    /*
	* @declare   fullname of  Users
	*/
    protected $fullname;
    
    /*
	* @declare   gender of  Users
	*/
    protected $gender;
    
    /*
	* @declare   email of  Users
	*/
    protected $email;
    
    /*
	* @declare   address of  Users
	*/
    protected $address;
    
    /*
	* @declare   telephone of  Users
	*/
    protected $telephone;
    
    /*
	* @declare   type of  Users
	*/
    protected $type;
    
    /*
	* @declare   last_login of  Users
	*/
    protected $last_login;
    
    /*
	* @declare   created of  Users
	*/
    protected $created;
    
    /*
	* @declare   status of  Users
	*/
    protected $status;
    
    
    /**
        All method of Users
    */
    
    /**
     * @desc this method will set the value of the $id property
     * @param int  $id
     * @return void
     */
     function setId($id)
     {
        $this->id = $id;
     } // end set function 
     
     /**
     * @desc this method will return the value of the $id property 
     * @param void
     * @return int of $id
     */
     function getId()
     {
        return  $this->id;
     } // end get function
     
    /**
     * @desc this method will set the value of the $username property
     * @param varchar  $username
     * @return void
     */
     function setUsername($username)
     {
        $this->username = $username;
     } // end set function 
     
     /**
     * @desc this method will return the value of the $username property 
     * @param void
     * @return varchar of $username
     */
     function getUsername()
     {
        return  $this->username;
     } // end get function
     
    /**
     * @desc this method will set the value of the $password property
     * @param varchar  $password
     * @return void
     */
     function setPassword($password)
     {
        $this->password = $password;
     } // end set function 
     
     /**
     * @desc this method will return the value of the $password property 
     * @param void
     * @return varchar of $password
     */
     function getPassword()
     {
        return  $this->password;
     } // end get function
     
    /**
     * @desc this method will set the value of the $fullname property
     * @param varchar  $fullname
     * @return void
     */
     function setFullname($fullname)
     {
        $this->fullname = $fullname;
     } // end set function 
     
     /**
     * @desc this method will return the value of the $fullname property 
     * @param void
     * @return varchar of $fullname
     */
     function getFullname()
     {
        return  $this->fullname;
     } // end get function
     
    /**
     * @desc this method will set the value of the $gender property
     * @param tinyint  $gender
     * @return void
     */
     function setGender($gender)
     {
        $this->gender = $gender;
     } // end set function 
     
     /**
     * @desc this method will return the value of the $gender property 
     * @param void
     * @return tinyint of $gender
     */
     function getGender()
     {
        return  $this->gender;
     } // end get function
     
    /**
     * @desc this method will set the value of the $email property
     * @param varchar  $email
     * @return void
     */
     function setEmail($email)
     {
        $this->email = $email;
     } // end set function 
     
     /**
     * @desc this method will return the value of the $email property 
     * @param void
     * @return varchar of $email
     */
     function getEmail()
     {
        return  $this->email;
     } // end get function
     
    /**
     * @desc this method will set the value of the $address property
     * @param varchar  $address
     * @return void
     */
     function setAddress($address)
     {
        $this->address = $address;
     } // end set function 
     
     /**
     * @desc this method will return the value of the $address property 
     * @param void
     * @return varchar of $address
     */
     function getAddress()
     {
        return  $this->address;
     } // end get function
     
    /**
     * @desc this method will set the value of the $telephone property
     * @param varchar  $telephone
     * @return void
     */
     function setTelephone($telephone)
     {
        $this->telephone = $telephone;
     } // end set function 
     
     /**
     * @desc this method will return the value of the $telephone property 
     * @param void
     * @return varchar of $telephone
     */
     function getTelephone()
     {
        return  $this->telephone;
     } // end get function
     
    /**
     * @desc this method will set the value of the $type property
     * @param int  $type
     * @return void
     */
     function setType($type)
     {
        $this->type = $type;
     } // end set function 
     
     /**
     * @desc this method will return the value of the $type property 
     * @param void
     * @return int of $type
     */
     function getType()
     {
        return  $this->type;
     } // end get function
     
    /**
     * @desc this method will set the value of the $last_login property
     * @param datetime  $last_login
     * @return void
     */
     function setLastLogin($last_login)
     {
        $this->last_login = $last_login;
     } // end set function 
     
     /**
     * @desc this method will return the value of the $last_login property 
     * @param void
     * @return datetime of $last_login
     */
     function getLastLogin()
     {
        return  $this->last_login;
     } // end get function
     
    /**
     * @desc this method will set the value of the $created property
     * @param datetime  $created
     * @return void
     */
     function setCreated($created)
     {
        $this->created = $created;
     } // end set function 
     
     /**
     * @desc this method will return the value of the $created property 
     * @param void
     * @return datetime of $created
     */
     function getCreated()
     {
        return  $this->created;
     } // end get function
     
    /**
     * @desc this method will set the value of the $status property
     * @param tinyint  $status
     * @return void
     */
     function setStatus($status)
     {
        $this->status = $status;
     } // end set function 
     
     /**
     * @desc this method will return the value of the $status property 
     * @param void
     * @return tinyint of $status
     */
     function getStatus()
     {
        return  $this->status;
     } // end get function
       
     
     
     /**
     * @desc this function get Users  by $id
     * @var int : $id 
     * @var 
     * @return Users object
     */
     function getById($id)
     {
          $row = $this->fetchRow("id='$id'");
          if($row)
            $this->fromArray($row->toArray());
          return $this;
     }
     
     /**
     * @desc this function get Users  by $username
     * @var varchar : $username 
     * @var 
     * @return Users object
     */
     function getByUsername($username)
     {
          $row = $this->fetchRow("username='$username'");
          if($row)
            $this->fromArray($row->toArray());
          return $this;
     }
     
     /**
     * @desc this function get Users  by $password
     * @var varchar : $password 
     * @var 
     * @return Users object
     */
     function getByPassword($password)
     {
          $row = $this->fetchRow("password='$password'");
          if($row)
            $this->fromArray($row->toArray());
          return $this;
     }
     
     /**
     * @desc this function get Users  by $fullname
     * @var varchar : $fullname 
     * @var 
     * @return Users object
     */
     function getByFullname($fullname)
     {
          $row = $this->fetchRow("fullname='$fullname'");
          if($row)
            $this->fromArray($row->toArray());
          return $this;
     }
     
     /**
     * @desc this function get Users  by $gender
     * @var tinyint : $gender 
     * @var 
     * @return Users object
     */
     function getByGender($gender)
     {
          $row = $this->fetchRow("gender='$gender'");
          if($row)
            $this->fromArray($row->toArray());
          return $this;
     }
     
     /**
     * @desc this function get Users  by $email
     * @var varchar : $email 
     * @var 
     * @return Users object
     */
     function getByEmail($email)
     {
          $row = $this->fetchRow("email='$email'");
          if($row)
            $this->fromArray($row->toArray());
          return $this;
     }
     
     /**
     * @desc this function get Users  by $address
     * @var varchar : $address 
     * @var 
     * @return Users object
     */
     function getByAddress($address)
     {
          $row = $this->fetchRow("address='$address'");
          if($row)
            $this->fromArray($row->toArray());
          return $this;
     }
     
     /**
     * @desc this function get Users  by $telephone
     * @var varchar : $telephone 
     * @var 
     * @return Users object
     */
     function getByTelephone($telephone)
     {
          $row = $this->fetchRow("telephone='$telephone'");
          if($row)
            $this->fromArray($row->toArray());
          return $this;
     }
     
     /**
     * @desc this function get Users  by $type
     * @var int : $type 
     * @var 
     * @return Users object
     */
     function getByType($type)
     {
          $row = $this->fetchRow("type='$type'");
          if($row)
            $this->fromArray($row->toArray());
          return $this;
     }
     
     /**
     * @desc this function get Users  by $last_login
     * @var datetime : $last_login 
     * @var 
     * @return Users object
     */
     function getByLastLogin($last_login)
     {
          $row = $this->fetchRow("last_login='$last_login'");
          if($row)
            $this->fromArray($row->toArray());
          return $this;
     }
     
     /**
     * @desc this function get Users  by $created
     * @var datetime : $created 
     * @var 
     * @return Users object
     */
     function getByCreated($created)
     {
          $row = $this->fetchRow("created='$created'");
          if($row)
            $this->fromArray($row->toArray());
          return $this;
     }
     
     /**
     * @desc this function get Users  by $status
     * @var tinyint : $status 
     * @var 
     * @return Users object
     */
     function getByStatus($status)
     {
          $row = $this->fetchRow("status='$status'");
          if($row)
            $this->fromArray($row->toArray());
          return $this;
     }
       
     
     // function save ".trimplural($table)." 
     public function save($forceInsert= false)
     { 
         if( $forceInsert == true)
            return $this->insert($this->toArray());
         $update = false;
         $condition = array(); 
        
        if($this->getId())
        {
            // is update
            $update = true;
            $condition[] = "id ='". $this->getId() ."'";
        }
        
         if( $update )   // update Users
         {
            return $this->update($this->toArray(),implode(" and ",$condition));
         }
         else
         {
             $this->insert($this->toArray());
         }
     }
     /**
     * @desc this function get list of ".$table."
     * @var int : string $where condition, $page is current page, $rowCount is total row
     * @var 
     * @return user object
     */
     public function getListUsers($where = null,$order = array(),$page = null ,$rowCount = null)
     {
        $select =  $this->select();
        if($where != null)
            $select->where($where);
         if($page===null)
             $select->from($this->_name);
        else
           $select->from($this->_name)->limitPage($page,$rowCount);
        $select->order($order);
        return $this->fetchAll($select);
     }     
     
     /**
     * @desc this method will import data from array 
     * @param void
     * @return array
     */
     function fromArray($array)
     {
        // import all proper ty
        
        if(isset($array["id"]))
                $this->id = $array["id"];
        
        if(isset($array["username"]))
                $this->username = $array["username"];
        
        if(isset($array["password"]))
                $this->password = $array["password"];
        
        if(isset($array["fullname"]))
                $this->fullname = $array["fullname"];
        
        if(isset($array["gender"]))
                $this->gender = $array["gender"];
        
        if(isset($array["email"]))
                $this->email = $array["email"];
        
        if(isset($array["address"]))
                $this->address = $array["address"];
        
        if(isset($array["telephone"]))
                $this->telephone = $array["telephone"];
        
        if(isset($array["type"]))
                $this->type = $array["type"];
        
        if(isset($array["last_login"]))
                $this->last_login = $array["last_login"];
        
        if(isset($array["created"]))
                $this->created = $array["created"];
        
        if(isset($array["status"]))
                $this->status = $array["status"];
        
        // return object
        return $this;
     } // end export function
     
     /**
     * @desc this method will export data to array 
     * @param void
     * @return array
     */
     function toArray()
     {
        // import all proper ty
        $array = array();
        
        $array["id"] = $this->id;
        
        $array["username"] = $this->username;
        
        $array["password"] = $this->password;
        
        $array["fullname"] = $this->fullname;
        
        $array["gender"] = $this->gender;
        
        $array["email"] = $this->email;
        
        $array["address"] = $this->address;
        
        $array["telephone"] = $this->telephone;
        
        $array["type"] = $this->type;
        
        $array["last_login"] = $this->last_login;
        
        $array["created"] = $this->created;
        
        $array["status"] = $this->status;
        
        // return object
        return $array;
     } // end export function
     
    function numRows($where = null)
    {
        $select = $this->_db->select();
        $select->from($this->_name, "COUNT(*)", $this->_schema);
        // the WHERE clause
        $where = (array) $where;
        foreach ($where as $key => $val) {
            // is $key an int?
            if (is_int($key)) {
                // $val is the full condition
                $select->where($val);
            } else {
                // $key is the condition with placeholder,
                // and $val is quoted into the condition              
                $select->where($key, $val);           
            }          
        }
        // return the results
        $stmt = $this->_db->query($select);
        
        $data = $stmt->fetchColumn(0);       
        return $data;  
    }              
}